//{==================================================================================================
//! @file "cpu_core.cpp"
//! @date 2013-11-09
//! @mainpage Virtual CPU console UI
//! @author Kirill Shcherbatov <kshcherbatov@gmail.com>
//! @version 2.0
//}===================================================================================================

//****************************************************************************************************
static int use_debug_out = 0;
//****************************************************************************************************

//****************************************************************************************************
#include <stdio.h>
//----------------------------------------------------------------------------------------------------
#include "cpu_t.cpp"
#include "defines.h"
#include <string.h>
//****************************************************************************************************	

//****************************************************************************************************
//! @param - number of sending arguments
//! @param argv[] - arguments, include generated by OS
//! @return 0 on success
//----------------------------------------------------------------------------------------------------
int main (int argc, char * argv[])
{
	OUT ( "# %s \n"
		  "# (%s)\n"
		  "# %s %s:\n\n",
		   argv[0], __FILE__, __DATE__, __TIME__ );
		   
	if ((argv[1] != NULL) && (argv[2] != NULL) && strcmp ("-d", argv[2]) == 0) use_debug_out = 1;
	
	printf ("CPU core emulator v 2.0 (author Shcherbatov Kirill) launched.\n");

	cpu_t *ilab_cpu = NULL;
	ilab_cpu = cpu_constructor();
	RETURN_ASSERT (ilab_cpu != NULL);

	printf ("Dynamic memory is allocated successfully. The variables are initialized.\n");

	char file_path[255] = {0};

	if (argv[1] == NULL)
	{
		printf ("Please enter the path to the instructions to execute:\n");
		scanf (" %s ", &file_path);
	}
	else strcpy (file_path, argv[1]);

	printf ("Trying to load program from file...\n");

	FILE *fin = NULL;
	fin = fopen (file_path, "r");

	RETURN_ASSERT (fin != NULL);
	RETURN_ASSERT (cpu_load_program (ilab_cpu, fin) == 0);

	printf ("Now start the program:\n");

	RETURN_ASSERT (cpu_run (ilab_cpu) == 0);

	printf ("\n"
	        "Succcess (0). End of the program.\n"
			"Releasing memory... ");

	RETURN_ASSERT ( cpu_destructor (ilab_cpu) == 0);

	printf ("OK.\n");

	return 0;
}
//****************************************************************************************************
